package com.example.ch8;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class PersonHasDog
 */
public class PersonDogServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PersonDogServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pName = request.getParameter("pName");
		String dName = request.getParameter("dName");
		Dog dog = new Dog(dName);
		
		ArrayList<Dog> dogs = new ArrayList<Dog>();
		dogs.add(dog);
		dogs.add(new Dog("TestDog1"));
		dogs.add(new Dog("TestDog2"));
		dogs.add(new Dog("TestDog3"));
		request.setAttribute("dogs", dogs);
		
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("dog0", "dog0Name");
		map.put("dog1", "dog1Name");
		map.put("dog2", "dog2Name");
		request.setAttribute("map", map);
		
		request.setAttribute("dog1", "dog1");
		
		PersonDog personDog = new PersonDog(pName, dog);
		request.setAttribute("personDog", personDog);
		
		PersonDog p = new PersonDog();
		p.setName("Leelu");
		Dog d = new Dog();
		d.setName("Clyde");
		Toy t1 = new Toy();
		t1.setName("stick");
		Toy t2 = new Toy();
		t2.setName("neighbor's cat");
		Toy t3 = new Toy();
		t3.setName("Barbie doll head");
		d.setToys(new Toy[]{t1, t2, t3});
		p.setDog(d);
		request.setAttribute("person", p);
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("getDogname.jsp");
		dispatcher.forward(request, response);
	}

}
